package kizzo.particle_system 
{
	import flash.events.Event;

	public class ParticleSystem 
	{
		private var particle_list:Vector.<Particle>;
		private var particle_cnt:int;
		
		public var MAX_PARTICLES:int = 100;
		public var generation_speed:Number = 10;
		
		
		public function ParticleSystem() 
		{
			this.particle_list = new Vector.<Particle>(MAX_PARTICLES);
			this.particle_cnt = 0;
		}
		
		
		public function start():void
		{
			Main.get_instance().addEventListener(Event.ENTER_FRAME, add_particles);
			Main.get_instance().addEventListener(Event.ENTER_FRAME, update);
		}
		
		
		public function stop():void
		{
			Main.get_instance().removeEventListener(Event.ENTER_FRAME, add_particles);
		}
		
		
		private function add_particles(e:Event):void
		{
			for (var i:Number = 0; i < generation_speed; i++)
			{
				if (particle_list[particle_cnt] != null)
					particle_list[particle_cnt].remove_from_scene();
					
				var particle:Particle = generate_particle();				
				particle.add_to_scene();
				particle_list[particle_cnt] = particle;
				
				particle_cnt++;
				particle_cnt %= MAX_PARTICLES;
			}
		}	
		
		
		private function update(e:Event):void
		{
			for (var i:int = 0; i < this.particle_list.length; i++)
			if (this.particle_list[i] != null)
				this.particle_list[i].update();
		}
		
		
		private function generate_particle():Particle
		{
			return new Particle(MaterialLib.MASTER_CUBE);
		}
	}

}